package com.jadobo.xswing.util;

import org.w3c.dom.Node;

import com.jadobo.xswing.attrib.HtmlElementTag;
import com.jadobo.xswing.model.Form;
import com.jadobo.xswing.model.IElement;

public class Doc {

	/**
	 * Get parent's local element name.
	 * 
	 * @param child
	 * @return
	 */
	public static String getParentElementName(Node child) {
		Node parent = child.getParentNode();
		if(parent == null) {
			return null;
		} 
		
		return parent.getNodeName();
	}

	/**
	 * Find the first occurrence of first level child. 
	 * 
	 * @param parent
	 * @param childElemName
	 * @return
	 */
	public static Node findChildElement(Node parent, String childElemName) {
		
		Iterable<Node> iterator = new NodeListAdapter(parent.getChildNodes());
		for(Node node : iterator) {
			if(node.getNodeName().equals(childElemName)) {
				return node;
			}
		}
		
		// not found!
		return null;
	}
	
	public static String getElementName(Node node) {
		if(node == null) {
			return null;
		}
		return node.getNodeName();
	}

	public static String getParentFormAction(IElement element) {
		if(element == null) {
			return null;
		}
		
		if(element instanceof Form) {
			
			Form form = (Form) element;
			
			return form.getAction();
		}
		
		return getParentFormAction(element.getParent());
	}
}
